package com.yjt.fresh.activity;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;

import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpVpFrg;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.fragment.order.FrgOrderAll;
import com.yjt.fresh.fragment.order.FrgOrderWaitComment;
import com.yjt.fresh.fragment.order.FrgOrderWaitPay;
import com.yjt.fresh.fragment.order.FrgOrderWaitReceive;
import com.yjt.fresh.fragment.order.FrgOrderWaitSend;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的订单
 * Created by mcc on 2018-07-27.
 */

public class AtyMyOrder extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.indicatorAppAtyMyOrder)
    MagicIndicator mIndicator;
    @BindView(R.id.vpAppAtyMyOrder)
    ViewPager mVp;
    private CommonNavigator mCommonNavigator;
    private CommonNavigatorAdapter mCommonNavigatorAdapter;
    private String[] mIndicatorList = {"全部","待付款","待发货","待收货","待评价"};
    private int mCurrentPage;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_my_order;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initVP();
        initIndicator();
        ViewPagerHelper.bind(mIndicator,mVp);
        mVp.setCurrentItem(getIntent().getIntExtra(MyConstants.POSITION,0)+1);
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initVP(){
        ArrayList<BaseSimpleFragment> frgOrderBases = new ArrayList<>();
        FrgOrderAll frgOrderAll = new FrgOrderAll();
        FrgOrderWaitSend frgOrderWaitSend = new FrgOrderWaitSend();
        FrgOrderWaitReceive frgOrderWaitReceive = new FrgOrderWaitReceive();
        FrgOrderWaitPay frgOrderWaitPay = new FrgOrderWaitPay();
        FrgOrderWaitComment frgOrderWaitComment = new FrgOrderWaitComment();
        frgOrderBases.add(frgOrderAll);
        frgOrderBases.add(frgOrderWaitPay);
        frgOrderBases.add(frgOrderWaitSend);
        frgOrderBases.add(frgOrderWaitReceive);
        frgOrderBases.add(frgOrderWaitComment);

        AdpVpFrg adpVpFrg = new AdpVpFrg(getSupportFragmentManager(),frgOrderBases);
        mVp.setAdapter(adpVpFrg);
        mVp.setCurrentItem(mCurrentPage);
        mVp.setOffscreenPageLimit(0);
    }
    private void initIndicator() {
        mIndicator.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        mCommonNavigator = new CommonNavigator(mContext);
        mCommonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mIndicatorList.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(mContext.getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(mContext.getResources().getColor(R.color.color_43b624));
                simplePagerTitleView.setText(mIndicatorList[i]);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mVp.setCurrentItem(i,false);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(mContext.getResources().getColor(R.color.color_009944));
                return linePagerIndicator;
            }
        };
        mCommonNavigator.setAdapter(mCommonNavigatorAdapter);
        mCommonNavigator.onPageSelected(mCurrentPage);
        mIndicator.setNavigator(mCommonNavigator);
    }
}
